﻿using System.Linq;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.FreeCellVariants
{
    [Game("48CBC4A6-384A-4744-BFA2-AF8BA8C7206C", LevelOfDifficulty.Medium, LevelOfSkill.MostlySkill, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class BakersGame : FreeCellBase
    {
        public BakersGame()
        {
            TotalTableaus = 8;
            TotalReserves = 4;
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var metrics = presentation.Metrics;

            var root = new SideBySideLayout
                       {
                           HorizontalContentAlignment = HorizontalAlignment.Stretch,
                           Direction = SpreadDirection.Vertical,
                           MinimumSpacing = metrics.MinSpacingDown,
                           MaximumSpacing = metrics.MinSpacingDown
                       };

            {
                var topRow = presentation.CreateRowLayout(); //CreateSideBySideLayout(metrics);

                var reserve = presentation.CreateRowLayout(); // CreateSideBySideLayout(metrics);
                var style = new VisualTraits
                            {
                                EmptyCue = () => EmptyPileCue.CardSite,
                                Arrangement = CardArrangement.StackSquare,
                                ContentFit = ContentFit.FitHeight
                            };

                foreach (var pile in PilesNamed(AnyReserve))
                    reserve.Add(presentation.CreatePileVisual(pile.Name, style));

                topRow.Add(reserve);

                var foundations = presentation.CreateRowLayout(); // CreateSideBySideLayout(metrics);

                presentation.AddPiles(foundations, PilesNamed(AnyFoundation), presentation.StyleForRepeatedRankFoundation(this, Rank.Ace, 1));

                //foreach (var name in PilesNamed(AnyFoundation).Select(pile => pile.Name))
                //    foundations.Add(presentation.CreatePileVisual(name, StandardStyles.Foundation));

                topRow.Add(foundations);

                root.Add(topRow);
            }

            {
                var tableau = presentation.CreateRowLayout(); // CreateSideBySideLayout(metrics);
                //tableau.VerticalContentAlignment = VerticalAlignment.Top;

                foreach (var pile in PilesNamed(AnyTableau))
                    tableau.Add(presentation.CreatePileVisual(pile.Name, StandardStyles.Tableau));

                root.Add(tableau);
            }

            return root;
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.DealToEach(NamesOfTableaus, "6 up");
            script.DealToEach(NamesOfTableaus.Take(4), "1 up");
        }
    }
}